<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

require(APPPATH."libraries/auth/Authenticator.php");

/**
 * LDAPAuthenticator
 *
 * @package     AuthenticatedController
 * @subpackage  LDAPAuthenticator
 * @author      Doug Everly
 */
class LDAPAuthenticator extends Authenticator {

    function __construct()
    {
        parent::__construct();
    }

    
    function login($username, $password)
    {
        $host   = $this->CI->config->item('ldap_host');
        $port   = $this->CI->config->item('ldap_port');
        $filter = $this->CI->config->item('ldap_filter');
        $dn     = sprintf($this->CI->config->item('ldap_dn', $username));

        $ds = ldap_connect($host, $port);
        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);

        if (@ldap_bind($ds, $dn, $password))
        {
            $search = ldap_search($ds, $dn, $filter, array('*', '+'));
            $entry = ldap_first_entry($ds, $search);
            
            if ($entry)
            {
                $this->make_session($username, $this->CI->config->item('ldap_group'));
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }
    
    

    
}

